package com.demo.ui.adapter;

import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.demo.bbdd.CursorUtils;
import com.demo.bbdd.NTContract;

public class PlacesAdapter extends CursorAdapter {

	public PlacesAdapter(Context context, Cursor c) {
		super(context, c, 0);
	}

	@Override
	public void bindView(View view, Context arg1, Cursor c) {
		ViewHolder holder = (ViewHolder) view.getTag();

		holder.text1.setText(CursorUtils.getString(c, NTContract.PLACE.NAME));
		holder.text2.setText(CursorUtils.getString(c, NTContract.PLACE.LAT)
				+ " / " + CursorUtils.getString(c, NTContract.PLACE.LNG));
	}

	@Override
	public View newView(Context arg0, Cursor arg1, ViewGroup arg2) {
		LayoutInflater inflater = LayoutInflater.from(arg0);

		View v = inflater.inflate(android.R.layout.simple_list_item_2, null);
		ViewHolder holder = new ViewHolder(v);

		v.setTag(holder);

		return v;
	}

	private class ViewHolder {
		public TextView text1;
		public TextView text2;

		public ViewHolder(View v) {
			text1 = (TextView) v.findViewById(android.R.id.text1);
			text2 = (TextView) v.findViewById(android.R.id.text2);
		}
	}
}
